<?php

namespace app\api\controller\store;

use app\api\model\GoodsCart;
use app\api\services\store\CartServices;
use app\api\validate\AddCartValidate;
use app\api\validate\UpdateCartValidate;
use app\common\controller\Api;
use think\db\exception\DataNotFoundException;
use think\db\exception\ModelNotFoundException;
use think\Exception;
use think\exception\DbException;


class Cart extends Api
{
    protected $noNeedRight = '*';

    /**
     * 添加购物车
     * @return void
     * @throws Exception
     * @throws DataNotFoundException
     * @throws ModelNotFoundException
     * @throws DbException
     */
    public function add()
    {
        $params = input();
        (new AddCartValidate())->goCheck();
        if ($params['number'] <= 0) {
            $this->error("库存必须大于0");
        }
        $userid = $this->auth->id;
        CartServices::getInstance()->add($userid, $params['goods_id'], $params['product_id'], $params['number'], $params['join_merchant_id']);
        $cartCount = GoodsCart::where('user_id', $userid)->sum('number');
        $this->success('成功', [
            'cartCount' => $cartCount
        ]);
    }

    /**
     * 立即购买
     * @return void
     * @throws DataNotFoundException
     * @throws DbException
     * @throws Exception
     * @throws ModelNotFoundException
     */
    public function fastAdd()
    {
        $params = input();
        (new AddCartValidate())->goCheck();
        $cart = CartServices::getInstance()->fastAdd($this->auth->id, $params['goods_id'], $params['product_id'], $params['number'], $params['join_merchant_id'],2);
        $this->success('成功', [
            'cartId' => $cart->id
        ]);
    }

    /**
     * 购物车列表
     * @return void
     * @throws DataNotFoundException
     * @throws DbException
     * @throws ModelNotFoundException
     */
    public function list()
    {
        $this->success('成功', CartServices::getInstance()->list($this->auth->id));
    }

    /**
     * 是否选中
     * @return void
     */
    public function checked()
    {
        $productIds = (array)input('productIds');
        $isChecked = input('isChecked');
        if (empty($productIds) || empty($isChecked)) {
            $this->error("参数错误");
        }
        $isChecked = $isChecked ? 1 : 0;
        try {
            GoodsCart::where('product_id', 'in', $productIds)
                ->update([
                    'checked' => $isChecked
                ]);
        } catch (\Exception $e) {
            $this->error("更新购物车失败");
        }
        $this->success($isChecked);
    }

    /**
     * 更新购物车件数
     * @return void
     * @throws Exception
     */
    public function update()
    {
        $params = input();
        (new UpdateCartValidate())->goCheck();
        CartServices::getInstance()->update($this->auth->id, $params['id'], $params['goodsId'], $params['productId'], $params['number']);
        $this->success();
    }

    /**
     * 删除购物车
     * @return void
     */
    public function delete()
    {
        $productIds = (array)input('productIds');
        if (empty($productIds)) {
            $this->error("参数错误");
        }
        try {
            GoodsCart::where('user_id', $this->auth->id)
                ->whereIn('product_id', $productIds)
                ->delete();
        } catch (\Exception $e) {
            $this->error("删除失败");
        }
        $this->success();
    }

    /**
     * 下单前信息确认
     * @return void
     * @throws DataNotFoundException
     * @throws DbException
     * @throws Exception
     * @throws ModelNotFoundException
     */
    public function checkout()
    {
        $cartIds = input('cartIds');
        $addressId = input('addressId');
        $couponId = input('couponId');
        if (empty($cartIds)) {
            $this->error("参数错误");
        }
        $res = CartServices::getInstance()->checkout($this->auth->id, $cartIds,$addressId,$couponId);
        $this->success($res);
    }
    public function coupons()
    {
        $countPrice = input('countPrice');
        var_dump($countPrice);
        if (empty($countPrice)){
            $this->error("参数错误");
        }

        $this->success('成功',CartServices::getInstance()->coupons($this->auth->id,$countPrice));
    }
}